<cfcomponent displayname="JsminJsCompressor" extends="AbstractCompressor" output="false" hint="Compresses JavaScript using JSMin">
	
	<cffunction name="init" access="public" returntype="AbstractCompressor" output="false">
		<cfargument name="javaLoader" type="any" required="false" default="" hint="a JavaLoader instance. If provided, this should be used to load the Java objects; if not provided, Java objects shouldbe instantiated via createObject()" />
		<cfargument name="jarPath" type="string" required="false" default="" hint="path of the directory where the .jar files are located" />
		<!--- Compressor options - see http://developer.yahoo.com/yui/compressor/#using for more details --->
		<cfargument name="munge" type="boolean" required="false" default="true" hint="if false: minify only, do not obfuscate" />
		<cfargument name="preserveSemiColons" type="boolean" required="false" default="false" hint="Preserve all semicolons?" />
		<cfargument name="disableOptimisations" type="boolean" required="false" default="false" hint="Disable all micro optimizations?" />
		
		<cfscript>
		var aJarPaths = arrayNew(1);
			
		if( isObject(arguments.javaLoader) AND len(arguments.jarPath) )
		{
			/*
				load external .jar classes using JavaLoader
			*/
			
			arrayAppend( aJarPaths, arguments.jarPath & '/yuicompressor-2.4.2.jar' );
			
			arguments.javaLoader.init( aJarPaths );		
			
			variables.jYuiJsCompressor = arguments.javaLoader.create("com.yahoo.platform.yui.compressor.JavaScriptCompressor");
		}
		else
		{
			/*
				assume external .jars are already available (in the classpath)
			*/
			
			variables.jYuiJsCompressor = createObject("java","com.yahoo.platform.yui.compressor.JavaScriptCompressor");
		}
		
		variables.jStringWriter = createObject("java","java.io.StringWriter");
		variables.jStringReader = createObject("java","java.io.StringReader");
		
		// compression options
		variables.bMunge = arguments.munge;
		variables.bPreserveSemiColons = arguments.preserveSemiColons;
		variables.bDisableOptimisations = arguments.disableOptimisations;
		
		super.init(argumentCollection=arguments);
		
		return this;
		</cfscript>
		
	</cffunction>
	
	<cffunction name="compressString" access="public" returnType="string" hint="takes a js string and minifies it using the YUI JS Compressor">
		<cfargument name="sInput" type="string" required="true" />
		<cfscript>
		var sOut = arguments.sInput;
		
		var joInput = variables.jStringReader.init(sOut);
		var joOutput = variables.jStringWriter.init();
		var joYUI = variables.jYuiJsCompressor.init(joInput,javaCast('null',''));

		// args: writer, linebreak, munge, verbose, preserve all semi-colons, disable optimisations
		joYUI.compress(
			joOutput,
			javaCast('int',-1),
			javaCast('boolean',variables.bMunge),
			javaCast('boolean',false),
			javaCast('boolean',variables.bPreserveSemiColons),
			javaCast('boolean',variables.bDisableOptimisations)
		);
		
		joInput.close();
		sOut = joOutput.toString();
		joOutput.close();
		
		return sOut;
		</cfscript>
		
	</cffunction>
	
</cfcomponent>
